class Solution {
    public int numDecodings(String s) {
        int n = s.length();
        int[] dp = new int[n + 1];
        char[] arr = s.toCharArray();
        if (n == 0 || arr[0] == '0') {
            return 0; // 如果字符串为空或以'0'开头，无法解码
        }
        dp[0] = 1;
        dp[1] = 1;
        // dp[i] = dp[i - 1] + dp[i - 2]
        for (int i = 2; i <= n; i++) {
            if (arr[i - 1] != '0' ) {
                dp[i] += dp[i - 1];
            }
            int x = (arr[i - 2] - '0') * 10 + arr[i - 1] - '0';
            if (x >= 10 && x <= 26) {
                dp[i] += dp[i - 2];
            }
        }
        return dp[n];
    }
}